package com.hibernate.dao.implement;

import java.util.List;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.hibernate.dao.DAODivisa;
import com.hibernate.model.Divisa;

/***
 * 
 * @author MARIA ALEJANDRA CELEMIN
 * @version 1.0
 * 
 * Clase que implementa los metodos de la interfaz DAODivisa
 * @see DAODivisa
 *
 */
public class DAODivisaImpl implements DAODivisa {

	private static Logger logger = Logger.getLogger(DAODivisaImpl.class);
	
	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAODivisa#obtenerDivisas()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Divisa> obtenerDivisas() {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		List<Divisa> encontradas = new ArrayList<Divisa>();
		
		try {
			encontradas = session.createQuery("from Divisa").list();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
		return encontradas;
	}

	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAODivisa#obtenerDivisa(String)
	 */
	@Override
	public Divisa obtenerDivisa(String codigo) {
	
		Session session = HibernateUtil.getSessionFactory().openSession();
		Divisa encontrada = null;
		
		try {
			encontrada = (Divisa) session.get(Divisa.class, codigo);
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
		return encontrada;
	}

	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAODivisa#guardarDivisa(com.hibernate.model.Divisa)
	 */
	@Override
	public void guardarDivisa(Divisa divisa) {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction();
		
		try {
			session.save(divisa);
			session.getTransaction().commit();
			
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}		
	}

	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAODivisa#eliminarDivisa(com.hibernate.model.Divisa)
	 */
	@Override
	public void eliminarDivisa(Divisa divisa) {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction();
		
		try {
			session.delete(divisa);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAODivisa#editarDivisa(com.hibernate.model.Divisa)
	 */
	@Override
	public void editarDivisa(Divisa divisa) {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction();
		
		try {
			session.merge(divisa);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

}
